<?php
if (!isset($load_data)) $load_data = array();
?>
<h3 class="xrvel_scaffolding_header">Edit <?php echo $xs_friendly_name; ?> Data</h3>
<?php if ($load_data == array()) : ?>
	<div class="xrvel_scaffolding_no_data">
		You have no data
	</div>
<?php else : ?>
	<?php
	//echo '<pre>';print_r($load_data);echo '</pre>';
	//echo '<pre>';print_r($xs_column_info);echo '</pre>';
	?>
	<form method="post" action="<?php echo current_url(); ?>">
	<table border="1" cellpadding="5" cellspacing="0" style="width:100%" class="xrvel_scaffolding_table_form">
		<?php foreach ($load_data as $name => $value) :
			$proceed = true;
			$display_name = xrvel_scaffolding_friendly_name($name);
			//echo '<pre>';print_r($xs_column_info);echo '</pre>';
			if (!isset($xs_column_info[$name]['load_from'])) {
				$display_name = $xs_column_info[$name]['friendly_name'];
			} else {
				$display_name = $xs_column_info[$name]['load_from']['friendly_name'];
			}
			$max_length = false;
			if (preg_match('/max\_length\[([0-9]+)\]/i', $xs_column_info[$name]['ci_rule'], $match)) {
				$max_length = $match[1];
			}
			if (isset($xs_column_info[$name]['additional_join']) && $xs_column_info[$name]['additional_join'] === true) {
				$proceed = false;
			} else if (isset($xs_column_info[$name]['primary_key']) && $xs_column_info[$name]['primary_key'] === true) {
				$proceed = false;
			} else if (isset($xs_column_info[$name]['editable']) && $xs_column_info[$name]['editable'] === false) {
				$proceed = false;
			} else if (isset($xs_column_info[$name]['hide_on']) && is_array($xs_column_info[$name]['hide_on'])) {
				if (in_array('edit', $xs_column_info[$name]['hide_on'])) {
					$proceed = false;
				}
			}
			if ($proceed) :
			?>
			<tr valign="top">
				<td class="xrvel_scaffolding_td_form_name">
					<?php echo $display_name; ?>
				</td>
				<td class="xrvel_scaffolding_td_form_value">
					<?php if (isset($xs_column_info[$name]['load_from'])) :
						$select = $name;
						if (isset($xs_column_info[$name]['load_from']['column'])) {
							$select = $xs_column_info[$name]['load_from']['column'];
						}
						$display = $xs_column_info[$name]['load_from']['display'];
						$where = '';
						if (isset($xs_column_info[$name]['load_from']['where']) && is_array($xs_column_info[$name]['load_from']['where']) && $xs_column_info[$name]['load_from']['where'] != array()) {
							$where = implode(' AND ', $xs_column_info[$name]['load_from']['where']);
							$where = " WHERE $where ";
						}
						$q = "SELECT $select, $display FROM ".$xs_column_info[$name]['load_from']['table']." $where ORDER BY $display ASC";
						$q = $this->db->query($q);
						$display_data = array();
						if ($q->num_rows() > 0) {
							foreach ($q->result_array() as $r) {
								$display_data[] = $r;
							}
						}
						?>
						<select name="<?php echo $name; ?>">
						<?php foreach ($display_data as $data) : ?>
						<option value="<?php echo $data[$select]; ?>" <?php echo set_select($name, $data[$select], ($data[$select] == $load_data[$name])); ?>><?php echo $data[$display]; ?></option>
						<?php endforeach; ?>
						</select>
					<?php else : ?>
						<?php if ($xs_column_info[$name]['type'] == 'text') : ?>
						<textarea name="<?php echo $name; ?>" rows="8" cols="60"><?php echo htmlentities($load_data[$name], ENT_COMPAT, 'UTF-8'); ?></textarea>
						<?php else : ?>
						<input type="text" name="<?php echo $name; ?>" value="<?php echo htmlentities($load_data[$name], ENT_COMPAT, 'UTF-8'); ?>" <?php if ($max_length !== false) { echo 'maxlength="'.$max_length.'"';} ?> />
						<?php endif; ?>
					<?php endif; ?>
					<?php echo form_error($name); ?>
					<?php if (isset($xs_column_info[$name]['edit_note'])) : ?>
						<div class="xrvel_scaffolding_note"><?php echo $xs_column_info[$name]['edit_note']; ?></div>
					<?php endif; ?>
				</td>
			</tr>
			<?php endif; ?>
		<?php endforeach; ?>
		<tr>
			<td class="xrvel_scaffolding_td_form_name">&nbsp;</td>
			<td><input type="submit" name="submit" value="Save" class="xrvel_scaffolding_submit" /> <input type="reset" value="Reset" class="xrvel_scaffolding_submit" /></td>
		</tr>
	</table>
	<input type="hidden" name="go" value="1" />
	</form>
	<?php echo validation_errors(); ?>
	<?php $this->load->view('xrvel_scaffolding/ok-error'); ?>
<?php endif; ?>
